package com.hello.action;

import java.awt.ItemSelectable;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.hello.pojo.Items;
import com.hello.pojo.QueryBean;
import com.hello.pojo.QueryVo;
import com.hello.service.ItemsService;

@Controller
public class ItemController {

	@Autowired
	private ItemsService itemsService;

	@RequestMapping("/sendJSON")
	@ResponseBody
	public Items sendJSON(@RequestBody Items items,Model model) throws Exception {
		System.out.println(items);
		model.addAttribute("result", items);
		return items;
	}

	@RequestMapping("/main")
	public String main(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage,
			Model model) throws Exception {
        model.addAttribute("pagemsg",itemsService.findByPage(currentPage));
		return "main";

	}

	@RequestMapping("/search")
	public String searchItem(QueryBean bean) throws Exception {

		// 通过 bean 获取到 items
		// 只要获取 items 就可以获取到 items 中所有的信息
		System.out.println("QueryBean--->" + bean);

		// 查商品的其他信息

		return "";
	}

	@RequestMapping("/delAll")
	public String delAll(QueryVo vo) throws Exception {

		Integer[] ids = vo.getIds();
		for (Integer id : ids) {
			itemsService.deleteItemsList(id);
		}
		return "success";

	}

	@RequestMapping("/updateItem")
	public String updateItem(MultipartFile pictureFile, Items items, Model model, HttpServletRequest request)
			throws Exception {
		// 文件上传
		String picName = pictureFile.getOriginalFilename();
		String newName = UUID.randomUUID().toString() + picName.substring(picName.lastIndexOf("."));
		File file = new File("C:/Users/40428/Desktop/pic/" + newName);
		pictureFile.transferTo(file);
		items.setPic(newName);

		items.setCreatetime(new Date());
		itemsService.updateItems(items);
		return "success";
	}

	@RequestMapping("/itemEdit/{id}")
	public String itemEdit(@PathVariable("id") Integer id, HttpServletRequest request) throws Exception {
//		Integer id = Integer.parseInt(request.getParameter("id"));
		Items items = itemsService.findItemById(id);
		request.setAttribute("item", items);
		return "editItem";

	}

	@RequestMapping("/list")
	public ModelAndView itemsList() throws Exception {

		List<Items> itemsList = itemsService.list();
		// 创建ModelAndView
		ModelAndView mv = new ModelAndView();
		mv.addObject("itemList", itemsList);
		mv.setViewName("itemList");
		return mv;

	}

}
